package lt.egidijus.aatest.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.PackageFragment;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;

@SuppressWarnings("restriction")
public class FileUtils {
	
	public static String getClassName(ICompilationUnit cUnit){
		String tempName =  cUnit.getElementName();
		if (tempName.lastIndexOf(Constants.testClassPostfix) > 0)
			return tempName.substring(0, tempName.lastIndexOf(Constants.testClassPostfix));
		else return tempName.substring(0, tempName.lastIndexOf(".java"));
	}
	
	public static void createCoverageFolder(){
		IProject iProject = ResourcesPlugin.getWorkspace().getRoot().getProject(Constants.testProjectName);
		IJavaProject project = JavaCore.create(iProject);	
		IFolder f = project.getProject().getFolder(	Constants.coverageResourcesFolder);
		if (!f.exists()) {
			try {
				f.create(true, true, null);
			} catch (CoreException e) {
				e.printStackTrace();
			}
		}
	}
	public static void deleteCoverageFolder(){
		IProject iProject = ResourcesPlugin.getWorkspace().getRoot().getProject(Constants.testProjectName);
		IJavaProject project = JavaCore.create(iProject);	
		IFolder f = project.getProject().getFolder(Constants.coverageResourcesFolder);	
		if (f.exists()) {
			try {
				f.delete(true, null);
			} catch (CoreException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static ArrayList<ICompilationUnit> getICompiliationUnit(Object o){
			
			ArrayList<ICompilationUnit> list = new ArrayList<ICompilationUnit>();
				if (o instanceof ICompilationUnit) {
					ICompilationUnit c = (ICompilationUnit) o;
					list.add(c);
				} else {
					if (o instanceof IPackageFragmentRoot) {
						IPackageFragmentRoot pack = (IPackageFragmentRoot) o;
						try {
							for (IJavaElement e : pack.getChildren()) {
								if (e instanceof PackageFragment) {
									PackageFragment p = (PackageFragment) e;
									list.addAll(Arrays.asList(p.getCompilationUnits()));
								}

							}
						} catch (JavaModelException e) {
							e.printStackTrace();
						}
					} else {
						if (o instanceof IPackageFragment) {
							IPackageFragment fragment = (IPackageFragment) o;
							try {
								list.addAll(Arrays.asList(fragment.getCompilationUnits()));
							} catch (JavaModelException e) {
								e.printStackTrace();
							}

						}
					}
			return list;
		}
		return null;		
	}
}
